﻿#ifndef SINGLEINSTANCEAPPLICATION_H
#define SINGLEINSTANCEAPPLICATION_H

#include <QApplication>
#include <QLocalServer>
#include <QLocalSocket>
#include <QWidget>

#if defined(qApp)
#undef qApp
#endif
#define qApp (static_cast<SingleInstanceApplication *>(QCoreApplication::instance()))

class SingleInstanceApplication : public QApplication
{
    Q_OBJECT
public:
    explicit SingleInstanceApplication(int &argc, char **argv);

    // 判断是否是主实例
    bool isPrimary() const { return m_isPrimary; }

    // 设置主窗口（用于显示已存在的实例）
    void setMainWindow(QWidget *window);

    // 重启实例
    void restart(const QStringList& args=QStringList());

signals:
    void requestShowUp(); // 当已有实例收到新请求时发出

private slots:
    void handleNewConnection();
    void activeMainWindow();

private:
    bool m_isPrimary = false;
    QString m_serverName;
    QLocalServer *m_server = nullptr;
    QWidget *m_mainWindow = nullptr;
};

#endif // SINGLEINSTANCEAPPLICATION_H
